package com.golf.client.course.renderer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import com.golf.core.course.Course;
import com.golf.core.course.bodies.Ball;
import com.golf.core.course.bodies.CourseBodies;
import com.golf.core.course.bodies.CourseBody;
import com.golf.core.course.bodies.CourseBoundary;
import com.golf.core.course.bodies.Hole;
import com.golf.core.course.bodies.PolyWall;
import com.golf.core.course.bodies.Position;
import com.golf.core.course.bodies.Wall;

public class CourseRenderer implements CanvasRenderer
{
    
    private final Course course;

    
    public CourseRenderer(final Course course)
    {
        this.course = course;
    }
	
	
	public void render(final Graphics g, final Dimension dim)
	{
	    // Render the course boundaries first.
	    CourseBoundary boundary = course.getBoundary();
	    g.setColor(boundary.color());
        g.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
        // NB: Fix above -> using window dims at the mo.
	    
	    CourseBodies bodies = course.getBodies();
	    synchronized (bodies)
        {
            //System.out.println("size: " +  bodies.getBodies().size());
    	    for (CourseBody body : bodies.getBodies())
    		{
                if (body instanceof Hole)
                {
                    Hole ball = (Hole)body;
                    Position p = ball.getPosition();
                    float r = Hole.HOLE_RADIUS;
                    
                    g.setColor(Color.BLACK);
                    g.fillOval(((int) p.getX()) - (int)r,((int) p.getY()) - (int)r,(int) (r*2),(int) (r*2));
                    continue;
                }
    		    else if (body instanceof Ball)
    		    {
    		        Ball ball = (Ball)body;
    		        Position p = ball.getPosition();
    		        float r = ball.getRadius();

                    g.setColor(Color.WHITE);
                    g.fillOval(((int) p.getX()) - (int)r,((int) p.getY()) - (int)r,(int) (r*2),(int) (r*2));

                    // THIS DRAWS THE BALL ROTATION - NOT USING THIS !!
                   // float rot = ball.getRotation();
                   // float xo = (float) (Math.cos(rot) * r);
                   // float yo = (float) (Math.sin(rot) * r);
                    
                    //g.setColor(Color.RED);
                    //g.drawLine((int) (p.getX()),(int) (p.getY()),(int) ( p.getX()+xo),(int) (p.getY()+yo));
                    //g.setColor(Color.BLACK);
                    continue;
    		    }
    		    else if (body instanceof Wall)
    		    {
                    Wall wall = (Wall)body;
        	        Position p = wall.getPosition();
                    float width = wall.getWidth();
                    float height = wall.getHeight();
    
                    g.setColor(wall.getBackground().getColour());
                    g.fillRect((int) p.getX(), (int) p.getY(), (int) width, (int) height);
                    continue;
    		    }
    		    else if (body instanceof PolyWall)
    		    {
    		    	final List<Position> vertices = ((PolyWall) body).getVertices();
    		    	final int[] xs = new int[vertices.size()];
    		    	final int[] ys = new int[vertices.size()];
    		    	int count = 0;
    		    	for (Position vertice : vertices)
    		    	{
    		    		xs[count] = (int)vertice.getX();
    		    		ys[count] = (int)vertice.getY();
    		    		count++;
    		    	}
    		    	
                    g.setColor(((PolyWall) body).getBackground().getColour());
                    g.fillPolygon(xs, ys, count);
                    continue;
    		    }
    		}
        
        }
		
		//We draw our image with its alpha channel set so that it is 
		//50% transparent (or 50% opaque, depending on how you look at it).
		//AlphaComposite myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f);
		//g2d.setComposite(myAlpha);
		//g2d.drawImage(myImage,atImageSpace,this);
	}
	
}
